package com.erp.module.jiaFang.xGJiaFang

import android.content.Context
import android.util.Log
import com.erp.bean.AddJiaFangBean
import com.erp.bean.JiaFangDXXBean
import com.erp.mclass.UnCeHandler
import com.erp.Interface.OnUserDataInterface
import com.erp.util.Httputil
import com.erp.util.JsonUtil
import com.erp.util.Myutil
import org.json.JSONException
import org.json.JSONObject

/**
 * Created by Administrator on 2018/7/4/004.
 * 作者：刘洋
 */
class IJiaFangKP(val KView : JiaFangKView, val context : Context, val id : String, val bean : JiaFangDXXBean) : JiaFangKP {


    override fun getJiaFangD() {
        UnCeHandler.buzhou = "getJiaFangD"
        Httputil.getJiaFangDanData(context,id,object : OnUserDataInterface {
            override fun onUserData(data: String?) {
//                Log.d("TAAG","data="+ Myutil.decodeUnicode(data))
                if(data==null){
                    return
                }
                val b =jsondata(data)
                if(b) {
                    KView.getJiaFangDOk()
                }else{
                    KView.getJiaFangDNo()
                }
            }

            override fun onNot(throwable: String?) {
//                Log.d("TAAG","throwable="+ Myutil.decodeUnicode(throwable))
                KView.getJiaFangDNo()
            }

        })
    }

    override fun shenHeJiaFang() {
        UnCeHandler.buzhou = "shenHeJiaFang"
        val jo =getshenhejo()
        Httputil.shenHeJiaFang(context,jo,id,object : OnUserDataInterface{
            override fun onUserData(data: String?) {
//                Log.d("TAAG","data="+Myutil.decodeUnicode(data))
                if(data==null){
                    KView.shenHeJiaFangNo()
                    return
                }
                val b =jsondata(data)
                if(b) {
                    KView.shenHeJiaFangOk()
                }else{
                    KView.shenHeJiaFangNo()
                }
            }

            override fun onNot(throwable: String?) {
//                Log.d("TAAG","throwable="+Myutil.decodeUnicode(throwable))
                KView.shenHeJiaFangNo()
            }

        })
    }

    override fun baoCunJiaFang(cangku: String, leixing: String) {
        UnCeHandler.buzhou = "baoCunJiaFang"
        when(cangku){
            "专卖店用品仓" ->  bean.iw_id="6"
            "中间仓" ->  bean.iw_id="5"
            "五金仓" ->  bean.iw_id="3"
            "其它材料仓" ->  bean.iw_id="2"
            "华标三区仓" ->  bean.iw_id="1"
            "原料仓" ->  bean.iw_id="4"
            "饰品仓" ->  bean.iw_id="7"
        }
        when(leixing){
            "入仓单" -> bean.type="-1"
            "出仓单" -> bean.type="1"
        }
        for(b in bean.jiafanglist){
            if((b.amount=="")||b.amount=="0"){
                Myutil.MyTost("货品数量不能为0")
                KView.baoCunJiaFangNo()
                return
            }
        }
        val jo =getbaocunjo()
        Httputil.xiuGaiFang(context,jo,id,object : OnUserDataInterface{
            override fun onUserData(data: String?) {
//                Log.d("TAAG","data="+Myutil.decodeUnicode(data))
                if(data==null){
                    KView.baoCunJiaFangNo()
                    return
                }
                val b =jsondata(data)
                if(b) {
                    KView.baoCunJiaFangOk()
                }else{
                    KView.baoCunJiaFangNo()
                }
            }

            override fun onNot(throwable: String?) {
//                Log.d("TAAG","throwable="+Myutil.decodeUnicode(throwable))
                KView.baoCunJiaFangNo()
            }

        })

    }

    override fun deleteJiaFang() {
        UnCeHandler.buzhou = "deleteJiaFang"
        Httputil.deleteJiaFang(context,bean.id,object : OnUserDataInterface{
            override fun onUserData(data: String?) {
//                Log.d("TAAG","data="+Myutil.decodeUnicode(data))
                KView.deleteJiaFangOk()
            }

            override fun onNot(throwable: String?) {
//                Log.d("TAAG","throwable="+Myutil.decodeUnicode(throwable))
                KView.deleteJiaFangNo()
            }

        })
    }
    //修改json
    fun getbaocunjo(): JSONObject{
        val jo = JSONObject()
        try {
            jo.put("iw_id",bean.iw_id)
            jo.put("iioo_type",bean.type)
            jo.put("remark",bean.remark)
            jo.put("audit_anyway",bean.audit_anyway)
            val jsgood=JSONObject()
            for(jiafangbean in bean.jiafanglist){
                val jogood = JSONObject()
                jogood.put("gi_id",jiafangbean.gi_id)
                jogood.put("amount",jiafangbean.amount)
                if(jiafangbean.oid=="0"){
                    jogood.put("oid", "")
                }else {
                    jogood.put("oid", jiafangbean.oid)
                }
                jsgood.put(jiafangbean.id,jogood)
            }
            val joo=JSONObject()
            joo.put("edit",jsgood)
            jo.put("goods",joo)
        }catch (e : JSONException){
            e.printStackTrace()
        }
        return jo
    }
    //审核json
    fun getshenhejo(): JSONObject {
        val jo = JSONObject()
        try {
            val joioorder = JSONObject()
            joioorder.put("id",bean.id)
            joioorder.put("type",bean.type)
            val joaudit = JSONObject()
            joaudit.put("iw_id",bean.iw_id)
            joaudit.put("auditor",bean.auditor)
            Log.d("TAAG",bean.audit_anyway)
            if( bean.audit_anyway=="1"){
                joaudit.put("audit_anyway","1")
            }
            jo.put("ioorder",joioorder)
            jo.put("audit",joaudit)
        }catch (e : JSONException){
            e.printStackTrace()
        }
        return jo
    }
    fun jsondata(data : String): Boolean{
        bean.jiafanglist.clear()
        try {
            val jo = JSONObject(data)
            bean.id= JsonUtil.getvolue(jo,"id")
            bean.inum= JsonUtil.getvolue(jo,"inum")
            bean.type= JsonUtil.getvolue(jo,"type")
            bean.iw_id= JsonUtil.getvolue(jo,"iw_id")
            bean.remark= JsonUtil.getvolue(jo,"remark")
            bean.ctime= JsonUtil.getvolue(jo,"ctime")
            bean.audit_time= JsonUtil.getvolue(jo,"audit_time")
            bean.audit_anyway= JsonUtil.getvolue(jo,"audit_anyway")
            bean.auditor_name= JsonUtil.getvolue(jo,"auditor_name")
            bean.creator_name= JsonUtil.getvolue(jo,"creator_name")
            bean.auditor= JsonUtil.getvolue(jo,"auditor")
            bean.creator= JsonUtil.getvolue(jo,"creator")
            val js = JsonUtil.getjsvolue(jo,"iiog")
            for(i in 0 until js.length()){
                val joo = js[i] as JSONObject
                val beanjiafang = AddJiaFangBean()
                beanjiafang.id= JsonUtil.getvolue(joo,"id")
                beanjiafang.gi_id= JsonUtil.getvolue(joo,"gi_id")
                beanjiafang.name= JsonUtil.getvolue(joo,"name")
                beanjiafang.code= JsonUtil.getvolue(joo,"code")
                beanjiafang.amount= JsonUtil.getvolue(joo,"amount")
                beanjiafang.size= JsonUtil.getvolue(joo,"size")
                beanjiafang.oid= JsonUtil.getvolue(joo,"oid")
                beanjiafang.o_onum= JsonUtil.getvolue(joo,"o_onum")
                bean.jiafanglist.add(beanjiafang)
            }
        }catch (e : JSONException){
            e.printStackTrace()
            Myutil.MyTost(data)
            return false
        }
        return true
    }
}